#include "XModStream.h"

XModFileStream::XModFileStream(){

}

XModFileStream::XModFileStream(string file){
	m_data = NULL;
	m_loaded = false;
	if (XModFile::Exist(file))
	if (m_file.OpenFile(file, FileMode::FM_READ_WRITE_BINARY))
		m_loaded = true;
}

XModFileStream::~XModFileStream(){
	m_file.Close();
	if (m_data != NULL)
		delete[] m_data;
}

void* XModFileStream::GetData(){
	if (!m_data&&(m_file.IsOpen())){
		int i_size = m_file.GetSize();
		m_data = new BYTE[i_size];
		fread((void*)m_data, i_size, 1,m_file.GetHandle());
	}
	return m_data;
}
void XModFileStream::Read(void* data, int size){
	if (data != NULL&&size != 0){
		fread((void*)data, size, 1, m_file.GetHandle());
	}
}
void XModFileStream::Close(){
	m_file.Close();
	m_loaded = false;
}
bool XModFileStream::IsEOF(){
	return !(m_file.GetSize() > ftell(m_file.GetHandle()));
}
void XModFileStream::Move(int des){
	fseek(m_file.GetHandle(), des, SEEK_CUR);
}
void XModFileStream::Seek(int pos){
	fseek(m_file.GetHandle(), pos, SEEK_SET);
}
int XModFileStream::Tell(){
	return ftell(m_file.GetHandle());
}

int XModFileStream::Size(){
	return m_file.GetSize();
}

bool XModFileStream::IsLoaded(){
	return m_loaded;
}